package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.web.controller.client.InterfaceController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 * 08bdb698-8cdf-4f49-a16e-fa5fb7b03b23
 */
@Controller
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    private String prefix = "system/dict/data";

    @Autowired
    private ISysDictDataService dictDataService;

    //6dea72f5-4abd-41bc-96f3-9d74b3ccac8e
    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String dictData()
    {
        return prefix + "/data";
    }

    //f24c2f29-e476-40f8-ba2d-4376a816306f
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    //34a4ce42-e9f3-4793-854c-9a562be58bef
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * 新增字典类型
     * b009aed5-550c-47cc-8dbe-15d25c8c0627
     */
    @GetMapping("/add/{dictType}")
    public String add(@PathVariable("dictType") String dictType, ModelMap mmap)
    {
        mmap.put("dictType", dictType);
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     * eb96e23e-efb9-4f18-84f8-7ff20fa4d5a6
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysDictData dict)
    {
        dict.setCreateBy(ShiroUtils.getLoginName());
//        return toAjax(dictDataService.insertDictData(dict));

        // 修改
        int num = dictDataService.insertDictData(dict);
        System.out.println(" -----  增加标准科室：" + dict.getDictType() );
        if( num >0 ){
            refreshControllerStandardKSlist();
        }
        return toAjax( num );
    }

    /**
     * 修改字典类型
     * 326cc904-8dae-47c9-8856-4fa0f9835dc9
     */
    @GetMapping("/edit/{dictCode}")
    public String edit(@PathVariable("dictCode") Long dictCode, ModelMap mmap)
    {
        mmap.put("dict", dictDataService.selectDictDataById(dictCode));
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     * 9703991d-4821-4350-be4f-8947421141bb
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysDictData dict)
    {
        dict.setUpdateBy(ShiroUtils.getLoginName());
//        return toAjax(dictDataService.updateDictData(dict));

        // 修改
        int num = dictDataService.updateDictData(dict);
        System.out.println(" -----  修改标准科室：" + dict.getDictType() );
        if( num >0 ){
            refreshControllerStandardKSlist();
        }
        return toAjax( num );
    }

    //59d5c152-e04b-4f67-ad6f-7dabf657ac79
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
//        return toAjax(dictDataService.deleteDictDataByIds(ids));

        // 修改
        int num = dictDataService.deleteDictDataByIds(ids);
        System.out.println(" -----  删除某标准科室！");
        if( num >0 ){
            refreshControllerStandardKSlist();
        }
        return toAjax( num );
    }


    // 刷新标准科室
    //ffbddb78-5c39-42c1-9771-49a79d554c8d
    private void refreshControllerStandardKSlist(){
        if( InterfaceController.standardKSlist != null ){
            InterfaceController.standardKSlist.clear();
            InterfaceController.standardKSlist.addAll( dictDataService.selectDictDataValueByType("yf_standard_dept_code") );
        }
    }




}
